﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TARADesktopDoctor
{
    /// <summary>
    /// Interaction logic for TreatmentSettings.xaml
    /// </summary>
    public partial class TreatmentSettings : UserControl
    {
        tara_wcf.ItaraServiceClient svc = new tara_wcf.ItaraServiceClient();
        public tara_wcf.EXE treatment = new tara_wcf.EXE();

        public TreatmentSettings()
        {
            this.InitializeComponent();
        }

        public void Show()
        {
            SetSettings();
            this.Visibility = Visibility.Visible;
        }

        private void SetSettings()
        {
            SettingsPanel.Children.Clear();
            #region record max/min setting
            int Max = 0;
            int Min = 0;
            int Max5 = 0;
            int Min5 = 0;
            int finalMax = 0;
            int finalMin = 0;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (gp.JointID == 4)
                {
                    switch (gp.GameKey)
                    {
                        case "Max Angle":
                            Max = Convert.ToInt32(gp.GameValue);
                            Max5 = Max + 5;
                            if (Max5 >= 180)
                            {
                                finalMax = 179;
                            }
                            else
                            {
                                finalMax = Max5;
                            }
                            break;
                        case "Min Angle":
                            Min = Convert.ToInt32(gp.GameValue);
                            Min5 = Min - 5;
                            if (Min5 <= 0)
                            {
                                finalMin = 1;
                            }
                            else
                            {
                                finalMin = Min5;
                            }
                            break;
                    }
                }
            }
            #endregion

            if (treatment.SettingsList == null)
            {
                foreach (string settings in treatment.CustKey)
                {
                    if (settings == "Max Angle")
                    {
                        TextBlock settingName = new TextBlock()
                        {
                            Text = settings,
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                            Margin = new Thickness(0, 0, 0, 0),
                            FontFamily = new FontFamily("Segoe WP"),
                            FontSize = 12
                        };
                        TextBox settingControl = new TextBox()
                        {
                            
                            Text = finalMax.ToString(),
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                            Margin = new Thickness(0, 0, 0, 8),
                            FontFamily = new FontFamily("Segoe WP")
                        };
                        SettingsPanel.Children.Add(settingName);
                        SettingsPanel.Children.Add(settingControl);
                    }
                    else if (settings == "Min Angle")
                    {
                        TextBlock settingName = new TextBlock()
                        {
                            Text = settings,
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                            Margin = new Thickness(0, 0, 0, 0),
                            FontFamily = new FontFamily("Segoe WP"),
                            FontSize = 12
                        };
                        TextBox settingControl = new TextBox()
                        {
                            Text = finalMin.ToString(),
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                            Margin = new Thickness(0, 0, 0, 8),
                            FontFamily = new FontFamily("Segoe WP")
                        };
                        SettingsPanel.Children.Add(settingName);
                        SettingsPanel.Children.Add(settingControl);
                    }
                    else
                    {
                        TextBlock settingName = new TextBlock()
                        {
                            Text = settings,
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                            Margin = new Thickness(0, 0, 0, 0),
                            FontFamily = new FontFamily("Segoe WP"),
                            FontSize = 12
                        };
                        TextBox settingControl = new TextBox()
                        {
                            Text = "",
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                            Margin = new Thickness(0, 0, 0, 8),
                            FontFamily = new FontFamily("Segoe WP")
                        };
                        SettingsPanel.Children.Add(settingName);
                        SettingsPanel.Children.Add(settingControl);
                    }
                }
            }
            else
            {
                //Populate the fields with previously entered data
                foreach (KeyValuePair<string, string> pair in treatment.SettingsList)
                {
                    TextBlock settingName = new TextBlock()
                    {
                        Text = pair.Key.ToString(),
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                        Margin = new Thickness(0, 0, 0, 0),
                        FontFamily = new FontFamily("Segoe WP"),
                        FontSize = 12
                    };
                    TextBox settingControl = new TextBox()
                    {
                        Text = pair.Value.ToString(),
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                        Margin = new Thickness(0, 0, 0, 8),
                        FontFamily = new FontFamily("Segoe WP")
                    };
                    SettingsPanel.Children.Add(settingName);
                    SettingsPanel.Children.Add(settingControl);
                }
            }
        }

        #region buttons
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            //assign the game to patient and save the settings to database
            int exe_patient_id = svc.assignedGametoPatient(MainPage.patientDetail.Uid, treatment.Uid);
            List<KeyValuePair<string, string>> tempList = new List<KeyValuePair<string, string>>();
            List<string> forTextBlock = new List<string>(); //Store settingKey
            List<string> forTextBox = new List<string>();   //Store settingValue
            KeyValuePair<string, string> temp = new KeyValuePair<string, string>();
            //Get value from the textbox
            foreach (var c in SettingsPanel.Children)
            {
                if (c.GetType() == typeof(TextBlock))
                {
                    TextBlock tbk = (TextBlock)c;
                    forTextBlock.Add(tbk.Text);
                }
                if (c.GetType() == typeof(TextBox))
                {
                    TextBox tbx = (TextBox)c;
                    if (tbx.Text != "")
                    {
                        forTextBox.Add(tbx.Text);
                    }
                    else
                    {
                        MessageBox.Show("Please fill in all fields");
                        break;
                    }
                }
            }
            bool filled = false;
            for (int i = 0; i < forTextBlock.Count; i++)
            {
                try
                {
                    temp = new KeyValuePair<string, string>(forTextBlock[i].ToString(), forTextBox[i].ToString());
                    tempList.Add(temp);
                }
                catch (ArgumentOutOfRangeException ex)    //There is empty field
                {
                    filled = true;
                }
            }
            if (filled == false)    //Do not create custKey if there is empty field
            {
                svc.createPatientExeCustkey(tempList, exe_patient_id);
                //Add to MainPage.assignedList
                foreach (tara_wcf.EXE exe in MainPage.exeList)
                {
                    if (exe.Uid == treatment.Uid)
                    {
                        exe.SettingsList = tempList;    //Add the setting to the exe object that is w/o setting
                        MainPage.assignedList.Add(exe);
                        break;
                    }
                }
            }
            this.Visibility = Visibility.Collapsed;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
            SettingsPanel.Children.Clear();
        }
        #endregion
    }
}